﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class RC_Personals : System.Web.UI.UserControl
    {
        IMasNamePrefixBLL prefixBLL = new MasNamePrefixBLL();
        IMasGenderBLL genderBLL = new MasGenderBLL();
        IMasNationalitiesBLL natBLL = new MasNationalitiesBLL();
        IMasReligionBLL religionBLL = new MasReligionBLL();
        IPersonalBLL personBLL = new PersonalBLL();

        private PersonalInfo personInfo;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            FillDataToGender();
            FillDataToNationalities();
            FillDataToReligion();

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblPersonSeq.Text = DataBinder.Eval(DataItem, "PERSON_SEQ").ToString();

                    personInfo = personBLL.GetPersonalInfo(lblPersonSeq.Text.Length > 0 ? Int32.Parse(lblPersonSeq.Text) : (int?)null);

                    cmbGender.SelectedValue = personInfo.GenderSeq.ToString();
                    FillDataToPrefixes(cmbGender.SelectedValue.Length > 0 ? Int32.Parse(cmbGender.SelectedValue) : (int?)null );
                    cmbPrefix.SelectedValue = personInfo.NamePrefixSeq.ToString();
                    txtFirstName.Text = personInfo.FirstName;
                    txtLastName.Text = personInfo.LastName;
                    txtFirstNameEng.Text = personInfo.FirstNameEng;
                    txtLastNameEng.Text = personInfo.LastNameEng;
                    txtBirthDate.Text = personInfo.BirthDate.ToString();
                    txtCitizen.Text = personInfo.Citizen;
                    cmbRaceNat.SelectedValue = personInfo.RaceNational.ToString();
                    cmbNat.SelectedValue = personInfo.National.ToString();
                    cmbReligion.SelectedValue = personInfo.Religion.ToString();
                    cmbBloodGroup.SelectedValue = personInfo.BloodGroup.ToString();
                    txtRemarks.Text = personInfo.Remarks;
                }

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void FillDataToGender()
        {
            //DataTable dt = gender.SelectDropDownList();

            //cmbGender.DataTextField = "GENDER_NAME";
            //cmbGender.DataValueField = "GENDER_SEQ";
            //cmbGender.DataSource = dt;
        }
        protected void FillDataToPrefixes(Nullable<int> GenderSeq)
        {
            IList<MasNamePrefixInfo> infoList = prefixBLL.ListNamePrefixes();

            cmbPrefix.DataTextField = "Name";
            cmbPrefix.DataValueField = "Sequence";
            cmbPrefix.DataSource = infoList;
            cmbPrefix.DataBind();
        }
        protected void FillDataToNationalities()
        {
            IList<MasNationalitiesInfo> infoList = natBLL.ListNationalities();

            cmbRaceNat.DataTextField = "NAT_NAME";
            cmbRaceNat.DataValueField = "NAT_SEQ";
            cmbRaceNat.DataSource = infoList;

            cmbNat.DataTextField = "NAT_NAME";
            cmbNat.DataValueField = "NAT_SEQ";
            cmbNat.DataSource = infoList;            
        }
        protected void FillDataToReligion()
        {
            IList<MasReligionInfo> infoList = religionBLL.ListReligions();

            cmbReligion.DataTextField = "Name";
            cmbReligion.DataValueField = "Sequence";
            cmbReligion.DataSource = infoList;
        }

        protected void cmbGender_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            FillDataToPrefixes(cmbGender.SelectedValue.Length > 0 ? Int32.Parse(cmbGender.SelectedValue) : (int?)null);
        }
        protected void SetPersonalInfo()
        {
            personInfo = new PersonalInfo();

            personInfo.PersonSeq = lblPersonSeq.Text.Length > 0 ? Int32.Parse(lblPersonSeq.Text) : (int?)null;
            personInfo.GenderSeq = cmbGender.SelectedValue.Length > 0 ? Int32.Parse(cmbGender.SelectedValue) : (int?)null;
            personInfo.NamePrefixSeq = cmbPrefix.SelectedValue.Length > 0 ? Int32.Parse(cmbPrefix.SelectedValue) : (int?)null;
            personInfo.FirstName = txtFirstName.Text.Length > 0 ? txtFirstName.Text : "";
            personInfo.LastName = txtLastName.Text.Length > 0 ? txtLastName.Text : "";
            personInfo.FirstNameEng = txtFirstNameEng.Text.Length > 0 ? txtFirstNameEng.Text : "";
            personInfo.BirthDate = txtBirthDate.Text.Length > 0 ? DateTime.Parse(txtBirthDate.Text) : (DateTime?)null;
            personInfo.Citizen = txtCitizen.Text.Length > 0 ? txtCitizen.Text : "";
            personInfo.RaceNational = cmbRaceNat.SelectedValue.Length > 0 ? Int32.Parse(cmbRaceNat.SelectedValue) : (int?)null;
            personInfo.National = cmbNat.SelectedValue.Length > 0 ? Int32.Parse(cmbNat.SelectedValue) : (int?)null;
            personInfo.Religion = cmbReligion.SelectedValue.Length > 0 ? Int32.Parse(cmbReligion.SelectedValue) : (int?)null;
            personInfo.BloodGroup = cmbBloodGroup.SelectedValue.Length > 0 ? cmbBloodGroup.SelectedItem.Text : "";
            personInfo.PersonalStatus = 1;
            personInfo.CreatedBy = 1;
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            SetPersonalInfo(); 
            personBLL.Insert(personInfo);
        }
        
        protected void btnUpdate_Click(object sender, EventArgs e)
        {            
            SetPersonalInfo();
            personBLL.Update(personInfo);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}